<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>商品列表</title>
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/module/admin.css?v=315"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #tableTbImg + .layui-table-view .layui-table-body tbody > tr > td {
            padding: 0;
        }

        #tableTbImg + .layui-table-view .layui-table-body tbody > tr > td > .layui-table-cell {
            height: 60px;
            line-height: 60px;
        }

        .tdImg {
            width: 50px;
            height: 50px;
            max-width: none;
            cursor: zoom-in;
        }

    </style>
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!--修改数据跳转页面-->
<script type="text/html" id="modelUser">
    <form id="modelUserForm" lay-filter="modelUserForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <input name="itemCid" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">商品标题</label>
            <div class="layui-input-block">
                <textarea name="itemTitle" placeholder="请输入商品标题" class="layui-textarea"
                       lay-verType="tips" lay-verify="required" required autocomplete="off" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
                <input class="layui-input" placeholder="请输入商品价格"
                       lay-verType="tips" lay-verify="number" name="itemPrice" autocomplete="off"/>
            </div>
        </div>
        <input name="itemImage" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">商品销量</label>
            <div class="layui-input-block">
                <input class="layui-input" placeholder="请输入商品销量"
                       lay-verType="tips" lay-verify="number" name="itemSales" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-block">
                <input name="itemCreatime" placeholder="请选择创建时间" id="test1" type="text" class="layui-input" autocomplete="off"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品状态</label>
            <div class="layui-input-block">
                <input type="radio" name="itemStats" value="1" title="正常" {{d.itemStats==1 ? "checked":''}}/>
                <input type="radio" name="itemStats" value="0" title="禁用" {{d.itemStats==0 ? "checked":''}}/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <div class="layui-input-block">
                <select name="c_name" id="updatecategory" lay-verType="tips" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="modelSubmitUser" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格内容 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
<!--            数据表格外部工具栏-->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">商品ID：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="itemId" id="itemId" placeholder="请输入商品ID" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">商品标题：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" id="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间：</label>
                        <div class="layui-input-inline">
                            <input id="timearea"  class="layui-input date-icon" type="text"
                                   placeholder="请选择查询时间段" autocomplete="off"/>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">商品状态：</label>
                        <div class="layui-input-inline">
                            <select name="state" id="state">
                                <option value="">所有</option>
                                <option value="1">正常</option>
                                <option value="0">锁定</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">商品分类：</label>
                        <div class="layui-input-inline">
                            <select name="category" id="category">
                                <option value="">所有商品</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-left: 20px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" data-type="reload" lay-submit >
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </div>
<!--            // 数据表格-->
            <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>

</div>
<!--// 数据表格中操作按钮的权限控制-->
<script type="text/html" id="barDemo">
    <shiro:hasPermission name="item:update">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    </shiro:hasPermission>
    <shiro:lacksPermission name="item:delete">
        <span class="layui-btn layui-btn-danger layui-btn-xs" >无权限</span>
    </shiro:lacksPermission>
    <shiro:hasPermission name="item:delete">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
    <shiro:lacksPermission name="item:delete">
        <span class="layui-btn layui-btn-danger layui-btn-xs" >无权限</span>
    </shiro:lacksPermission>
</script>
<!--数据表格中工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <shiro:hasPermission name="item:delete">
            <button class="layui-btn layui-btn-sm" lay-event="delete">删除选中</button>
            <button class="layui-btn layui-btn-sm" lay-event="fzall">封禁选中</button>
            <button class="layui-btn layui-btn-sm" lay-event="startall">开启选中</button>
        </shiro:hasPermission>
        <shiro:lacksPermission name="item:delete">
            <button class="layui-btn layui-btn-danger" disabled>无权限</button>
        </shiro:lacksPermission>
    </div>
</script>
<!--数据表格中对于状态的按钮-->
<script type="text/html" id="item-status">
    <shiro:hasPermission name="item:update">
        <input type="checkbox"  value="{{d.id}}" lay-skin="switch" lay-text="开启|禁用" lay-filter="itemIs" {{d.itemStats==1 ? 'checked':''}}>
    </shiro:hasPermission>
    <shiro:lacksPermission name="item:update">
        {{#
        var status = {
        1: {title: '正常', color: 'green'},
        0: {title: '锁定', color: 'volcano'}
        }[d.itemStats];
        }}
        <span class="layui-btn layui-btn-{{status.color}} layui-btn-xs" >{{status.title}}</span>
    </shiro:lacksPermission>
</script>
<script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/assets/js/common.js?v=315"></script>
<script>
    layui.use(['laydate', 'form','table','notice','admin','util'],function() {
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var notice = layui.notice;
        var util = layui.util;
        var laydate = layui.laydate;

        //时间选择器
        laydate.render({
            elem: '#timearea'
            , range: true
            ,trigger: 'click' //采用click弹出
        });

        var talbeIns=table.render({
            elem: '#tableTbBas'
            ,url:'/item/list'
             ,cellMinWidth: 100
            ,skin:"layui-layer-admin"
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['print', 'exports', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '刷新'
                ,layEvent: 'reset'
                ,icon: 'layui-icon-refresh'
            }]
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field:'id',align: 'center', title: 'ID',sort:true}
                    ,{
                        align: 'center', templet: function (d) {
                            var url = d.itemImage;
                            return '<img src="' + url + '" class="tdImg" tb-img/>';
                        }, title: '商品图片',unresize: true
                    }
                    ,{field:'c_name',  title: '所属分类',align: 'center'}
                    ,{field:'itemTitle',  title: '商品标题', align: 'center'}
                    ,{field:'itemPrice', title: '商品价格', sort: true, align: 'center'}
                    ,{field:'itemSales', title: '商品销量', sort: true, align: 'center'}
                    ,{field: 'itemCreatime', sort: true,align: 'center', templet: function (d) {
                            return util.toDateString(d.itemCreatime);
                        }, title: '创建时间'
                    }
                    ,{field: 'itemStats', align: 'center', title: '状态',templet: '#item-status'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center', width:150}
                ]
            ]
            ,page: true
            ,limit:10
            , limits: [ 5, 10, 15, 20,25]
            , loading: true
        });
        //表单刷新
        var $ = layui.$, active = {
            reload: function(){
                var timeArea = $("#timearea").val();
                var itemId=$("#itemId").val();
                var title=$("#title").val();
                var category= $("#category").find("option:selected").val();
                var stats= $("#state").find("option:selected").val();
                var startTime = "";
                var endTime = "";
                if (timeArea) {
                    startTime = timeArea.split(" - ")[0];//开始时间
                    endTime = timeArea.split(" - ")[1];//结束时间
                }
                //执行重载
                table.reload('tableTbBas', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        itemId:itemId,
                        title:title,
                        cId:category,
                        stats:stats,
                        startTime:startTime,
                        endTime:endTime
                    }
                }, 'data');
            }
        };
        //头工具栏事件
        table.on('toolbar(tableTbBas)', function(obj){
            if (obj.event==='delete'){
                var checkStatus = table.checkStatus(obj.config.id);
                if (!checkStatus.data.length){
                    notice.msg("请选择要删除的数据！！",{icon:5,timeout:2000,animateInside:true,audio:'1'});
                }else{
                    layer.confirm('确定删除多条数据？',{icon:3, title:'提示信息',skin:'layui-layer-admin'},function(index){
                        var logIds = [];
                        layui.each(checkStatus.data, function (key, item) {
                            logIds.push(item.id);
                        });
                        $.get('/item/delete/'+logIds,function(data){
                            if (data.code==200){
                                talbeIns.reload();
                                layer.close(index);
                                notice.msg("删除多条商品成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                            } else{
                                notice.msg(data.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                            }
                        });
                    });
                }
            }
            if (obj.event==='reset'){
                talbeIns.reload();
            }
            if (obj.event==='fzall'){//封禁
                var checkStatus = table.checkStatus(obj.config.id);
                if (!checkStatus.data.length) {
                    notice.msg("请选择要封禁的商品！！", {icon: 5, timeout: 2000, animateInside: true, audio: '1'});
                }else{
                    var logIds = [];
                    layui.each(checkStatus.data, function (key, item) {
                        logIds.push(item.id);
                    });
                    $.ajax({
                            url:"/item/updateStatsAll/"+logIds+"/false",
                            type:"post",
                            async:false,
                            dataType:"json",/**这个类型很重要 */
                            cache:false,
                            success:function (data) {
                                if (data.code===200){
                                    talbeIns.reload();
                                    notice.msg("封禁成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                                } else{
                                    notice.msg(data.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                                }
                            }
                    });
                }
            }
            if (obj.event==="startall"){//开启
                var checkStatus=table.checkStatus(obj.config.id);
                if (!checkStatus.data.length) {
                    notice.msg("请选择要开启的商品！！", {icon: 5, timeout: 2000, animateInside: true, audio: '1'});
                }else{
                    var logIds = [];
                    layui.each(checkStatus.data, function (key, item) {
                        logIds.push(item.id);
                    });
                    $.ajax({
                        url:"/item/updateStatsAll/"+logIds+"/true",
                        type:"post",
                        async:false,
                        dataType:"json",
                        cache:false,
                        success:function (data) {
                            if (data.code===200){
                                talbeIns.reload();
                                notice.msg("开启成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                            } else{
                                notice.msg(data.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                            }
                        }
                    });
                }
            }
        });
        //监听行工具事件
        table.on('tool(tableTbBas)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                    layer.confirm('确定删除该数据吗？',{icon:3, title:'提示信息',skin:'layui-layer-admin'}, function(index){
                    $.get("/item/delete/"+obj.data.id,{},function (data) {
                        if (data.code){
                            layer.close(index);
                            talbeIns.reload();
                            notice.msg("删除商品成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                        } else{
                            notice.msg(data.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                        }
                    })
                });
            }
            if (obj.event==='edit'){
                showEditModel(data);
            }
        });

        // 显示表单弹窗
        function showEditModel(item) {
            layer.open({
                type: 1,
                title: '修改商品',
                area: [$(window).width() <= 750 ? '95%' : '500px'],
                content: $('#modelUser').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    findselect('#updatecategory');
                    laydate.render({//日期
                        elem: '#test1'
                        ,type: 'datetime'
                        ,trigger: 'click' //采用click弹出
                    });
                    // 回显数据
                    item.itemCreatime=layui.util.toDateString(item.itemCreatime);
                    form.render("select");    //重新渲染 固定写法
                    form.val('modelUserForm', item);
                    // 表单提交事件
                    form.on('submit(modelSubmitUser)', function (data) {
                        $.post('/item/update', data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code === 200) {
                                layer.close(dIndex);
                                talbeIns.reload();
                                notice.msg("修改商品成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                            } else {
                                notice.msg(res.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        // 点击图片放大
        $(document).off('click.tbImg').on('click.tbImg', '[tb-img]', function () {
            layer.photos(
                {
                    photos: {
                        data: [{src: $(this).attr('src')}]
                    },
                    shade: .1,
                    closeBtn: true,
                }
                );
        });

        //点击搜索按钮
        form.on("submit(formSubSearchLog)",function (data) {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听开启和禁用操作
        form.on('switch(itemIs)', function(obj){
            var flag=obj.elem.checked;
            $.post("/item/updateStatus",{"itemId":this.value,"flag":flag},function (data) {
                if (data.code==200&&flag==true){
                    notice.msg("开启成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                }else if(data.code==200&&flag==false){
                    notice.msg("禁用成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                }else{
                    notice.msg(data.message,{icon:2,timeout:2000,animateInside:true,audio:'1'});
                }
            });
        });

        findselect('#category');
        function findselect(id){
            //检查项目添加到下拉框中
            $.ajax({
                url: '/category/list',
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    $.each(data, function (index, item) {
                        $(id).append(new Option(item.cName, item.cId));// 下拉菜单里添加元素
                    });
                    form.render("select");    //重新渲染 固定写法
                }
            });
        }
    });

</script>
</body>
</html>